package pl.koziolekweb.szkola.KWDaneOb;

// KW-DaneOb/DaneOb.java
 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class DaneOb {

    public static void main(String[] args) {
        new DaneOb();
    }

    DaneOb() {

        Osoba[] t =
            {
                new Osoba("Ola",1988),new Osoba("Ala",1990),
                new Osoba("Ula",1989),new Osoba("Ela",1983)
            };

        info("Przed zapisem",t);

          // zapis
        try {
            ObjectOutputStream oos =
                new ObjectOutputStream(
                    new FileOutputStream("Dane.dat"));
            oos.writeObject(t);
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }

          // odczyt
        try {
            ObjectInputStream ois =
                new ObjectInputStream(
                    new FileInputStream("Dane.dat"));
            t = (Osoba[])ois.readObject();
            ois.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        }

        info("Po   odczycie",t);
    }

    private void info(String mes,Osoba[] t) {
        System.out.print(mes + ":");
        for (Osoba o : t)
            System.out.print(" " + o);
        System.out.println();
    }
}
